import hashlib
import time
# from time import time
from random import randint,sample
from bs4 import BeautifulSoup

import requests
from requests.models import Response


def rand_ints(a, b):
    res = ''
    for _ in range(b):
        res += str(randint(0, a))
    return res


def rand_strings(n):
    res = ''
    for _ in range(n):
        res += chr(65 + randint(0, 25))
    return res


def nowtime():
    return str(int(time.time()))


def get_MD5(string):
    hmd5 = hashlib.md5()
    hmd5.update(string.encode(encoding='utf-8'))

    return hmd5.hexdigest()


def sign():
    # action = "".join([str(randint(1,9)) for _ in range(5)])
    action = rand_ints(9, 5)
    now = nowtime()
    randstr = rand_strings(5)

    _sign = action + now + randstr

    return {
        'actions': action,
        'tim': now,
        'randstr': randstr,
        'sign': get_MD5(_sign),
    }


# url = 'http://121.36.147.254/movies_sign_ajax'
url = 'http://47.103.13.124:8001/movies_sign_ajax'

cookies = {
    'session': '.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMDorQ.zWGKV8Qf0bkHy1zr8ZIpU6BgIVs; auth=814QVOED800818NOB1623330769',
}


def run():
    # r = sign()
    # url = 'http://47.103.13.124:8001/movies_sign_ajax?actions={}$tim={}&ran'
    # response = requests.get(url)
    response = requests.get(url=url, cookies=cookies, params=sign())
    r = response.json()
    # r = BeautifulSoup(response.text,'lxml')
    print(r)


if __name__ == '__main__':
    run()
